<template>
  <div class="bottom">
    <cart-button class="button" @click.native="selectAll" :checked="!isAllCheck"></cart-button>
    <div class="select">全选</div>
    <div class="count">合计:￥{{sumPrice}}</div>
    <div class="compute">去计算({{count}}）</div>
  </div>
</template>

<script>
  import CartButton from "./CartButton";

  export default {
    name: "CartBottomBar",
    props:{
      items:Array,
      count:Number
    },
    components:{
      CartButton
    },
    methods:{
      selectAll(){
        // 1.判断是否有未选中的按钮
        let isSelectAll = this.$store.getters.cartList.find(item => !item.checked);

        // 2.有未选中的内容, 则全部选中
        if (isSelectAll) {
          this.$store.state.cartList.forEach(item => {
            item.checked = true;
          });
        } else {
          this.$store.state.cartList.forEach(item => {
            item.checked = false;
          });
        }
      }
    },
    computed:{
      sumPrice() {
        const cartList = this.items;
        return cartList.filter(item => {
          return item.checked
        }).reduce((preValue, item) => {
          return preValue + item.count * item.price
        }, 0).toFixed(2)
      },
      isAllCheck(){
        return this.$store.getters.cartList.find(item => !item.checked)
      }
    }
  }
</script>

<style scoped>
  .de-select-all{
    background-color: #fff;
  }
  .button{
    position: relative;
    left: 10px;
  }
  .select{
    position: relative;
    left: 20px;
  }
  .count{
    position: relative;
    left: 40px;
  }
  .compute{
    color: #fff;
    background-color: orangered;
    position: absolute;
    right: 0;
    text-align: center;
    width: 100px;
    height: 50px;
    line-height: 50px;
    align-self: center;
  }
  .bottom{
    align-items: center;
    display: flex;
    position: absolute;
    bottom: 50px;
    height: 50px;
    background-color: rgba(100,100,100,0.2);
    width: 100%;
    font-size: 15px;
  }
</style>